﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kill_PC
{
    public partial class mainScreen : Form
    {
        public mainScreen()
        {
            InitializeComponent();
        }

        Int64 minute = 0, seconds = 0, tickLeft = 0;
        Timer t = new Timer();

        [DllImport("user32")]public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
        [DllImport("user32")]public static extern void LockWorkStation();
        private void buttonShut_Click(object sender, EventArgs e)
        {
            killPC(Convert.ToDouble(textBoxTime.Text.Replace(".",",")));
        }

        private void killPC(double min)
        {
            textBoxTime.Text = min.ToString();
            t.Stop();
            t = new Timer();
            t.Interval = 1000;
            minute = Convert.ToInt32(Math.Floor(min));
            seconds = Convert.ToInt32((min%1)*60);
            tickLeft = minute * 60 + seconds;
            t.Tick += new EventHandler(timer_tick);
            t.Start();
        }

        private void timer_tick(object sender, EventArgs a)
        {
            labelMinute.Text = minute.ToString();
            labelSecond.Text = seconds.ToString();
            if (tickLeft == 0)
            {
                t.Stop();
                    if(radioShutDown.Checked)
                    {
                        Process.Start("shutdown", "/s /t 0");
                    }
                    if(radioLogOff.Checked)
                    {
                        ExitWindowsEx(0,0);
                    }
                    if(radioLockComp.Checked)
                    {
                        LockWorkStation();
                    } 
            }//if(minute == 0 && seconds == 0)
            tickLeft--;
            minute = (Int64)(tickLeft / 60);
            seconds = tickLeft % 60;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            t.Interval = 1000;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            unCheckAll();
            t.Stop();
            t = new Timer();
            t.Interval = 1000;
        }

        private void textBoxTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar >= 47 && (int)e.KeyChar <= 57)
            {
                e.Handled = false;
            }
            else if ((int)e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else if ((int)e.KeyChar == 44 || (int)e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void sd30minuteLaterTSMI_Click(object sender, EventArgs e)
        {
            unCheckAll();
            sd30minuteLaterTSMI.Checked = true;
            radioShutDown.Checked = true;
            radioLockComp.Checked = false;
            radioLogOff.Checked = false;
            killPC(30);
        }

        private void unCheckAll()
        {
            foreach (ToolStripMenuItem cmsItem in cmsKillPC.Items)
            {
                foreach (ToolStripMenuItem dropDownItem in cmsItem.DropDownItems)
                {
                    dropDownItem.Checked = false;
                }
            }
        }

        private void niKillPC_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void mainScreen_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                this.Visible = false;
                niKillPC.ShowBalloonTip(100);
            }

            else if (FormWindowState.Normal == this.WindowState)
            {
                this.Visible = true;
            }
        }

        private void shutDownToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            dropDownClicked(sender,e);
        }

        private void logToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            dropDownClicked(sender, e);
        }

        private void lockYourComputerToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            dropDownClicked(sender, e);
        }

        private void dropDownClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            unCheckAll();
            ((ToolStripMenuItem)(e.ClickedItem)).Checked = true;
            string lTest = (e.ClickedItem).OwnerItem.Name.Substring(0, 3);
            switch (lTest)
            {
                case "shu": radioShutDown.Checked = true;
                    break;
                case "log": radioLogOff.Checked = true;
                    break;
                case "loc": radioLockComp.Checked = true;
                    break;
                default:
                    break;
            }

            switch (((ToolStripMenuItem)(e.ClickedItem)).ToolTipText)
            {
                case "30": killPC(30);
                    break;
                case "45": killPC(45);
                    break;
                case "60": killPC(60);
                    break;
                case "120": killPC(120);
                    break;
                default: killPC(5);
                    break;
            }
        }

        private void pictureBoxKillPC_Click(object sender, EventArgs e)
        {
            AboutBox aboutBox = new AboutBox();
            aboutBox.ShowDialog();
        }

    }
}

